# common/utils/decorators.py
from flask import g


# 强制登录装饰器
def login_reqiured(view_func):
    def wrapper(*args, **kwargs):
        user_id = g.user_id
        is_refresh = g.is_refresh
        # 1.判断用户是否登录
        if user_id is None:
            # 用户未登录
            # 401 权限认证失败
            return {"message": "invalid token"}, 401
        # 2.判断是否是刷新token
        elif user_id is not None and is_refresh:
            # 刷新token
            return {"message": "refresh token not for login", 'code': 2002}, 403
        else:
            # 登录token
            return view_func(*args, **kwargs)

    return wrapper
